/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package _bai02_phantichphankhaibao;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class jClass_BieuThucLogic implements Interface_CacThanhPhanDacTa{
    private String _DoiTuong1 = "";
    private String _PhepToan = "";
    private String _DoiTuong2 = "";
    private static String _BieuThucDinhDang = "(\\(\\s*"+jEnum_ChuoiDinhDang.GiaTri.value()
            + "\\s*" + jEnum_ChuoiDinhDang.CacToanTuLogic.value()
            + "\\s*" + jEnum_ChuoiDinhDang.GiaTri.value() + "\\s*\\))|(\\s*"+jEnum_ChuoiDinhDang.GiaTri.value()
            + "\\s*" + jEnum_ChuoiDinhDang.CacToanTuLogic.value()
            + "\\s*" + jEnum_ChuoiDinhDang.GiaTri.value() + "\\s*)";

    //private static String _BieuThucDinhDang = "\\(.*\\)?.*";
    // <editor-fold defaultstate="collapsed" desc="getter and setter">
    
    public jClass_BieuThucLogic(){
    }

    /**
     * @return the _DoiTuong1
     */
    public String getDoiTuong1() {
        return _DoiTuong1;
    }

    /**
     * @param DoiTuong1 the _DoiTuong1 to set
     */
    public void setDoiTuong1(String DoiTuong1) {
        this._DoiTuong1 = DoiTuong1;
    }

    /**
     * @return the _PhepToan
     */
    public String getPhepToan() {
        return _PhepToan;
    }

    /**
     * @param PhepToan the _PhepToan to set
     */
    public void setPhepToan(String PhepToan) {
        this._PhepToan = PhepToan;
    }

    /**
     * @return the _DoiTuong2
     */
    public String getDoiTuong2() {
        return _DoiTuong2;
    }

    /**
     * @param DoiTuong2 the _DoiTuong2 to set
     */
    public void setDoiTuong2(String DoiTuong2) {
        this._DoiTuong2 = DoiTuong2;
    }

    /**
     * @return the _BieuThucDinhDang
     */
    public static String getBieuThucDinhDang() {
        return _BieuThucDinhDang;
    }

    // </editor-fold>
    
    public Boolean jKiemTraChuoiHopDinhDang (String str_ChuoiCanKienTra)    {
        //String str_ChuoiDaBoSungDauPhanCach = str_ChuoiCanKienTra;
             //str_ChuoiDaBoSungDauPhanCach = str_ChuoiCanKienTra.replaceAll("\\)", ",\\)");
        Pattern pat=Pattern.compile(_BieuThucDinhDang);
        Matcher matcher =pat.matcher(str_ChuoiCanKienTra);
        return matcher.matches();
    }
    public void jCapNhatTuChuoi(String Str_BieuThucLogic) {
        Str_BieuThucLogic = Str_BieuThucLogic.trim();
        //if (Str_BieuThucLogic.)
        if (!jKiemTraChuoiHopDinhDang(Str_BieuThucLogic)){
            String mess = "Chuoi:\n" + Str_BieuThucLogic + "\n khong hop dinh dang cua mot Bieu Thuc Logic!!!";
            JOptionPane.showMessageDialog(null, mess);
            throw new ExceptionInInitializerError(Str_BieuThucLogic);
        }
        Str_BieuThucLogic = Str_BieuThucLogic.trim();
        if (Str_BieuThucLogic.contains("<"))
            _PhepToan += "<";
        if (Str_BieuThucLogic.contains(">"))
            _PhepToan += ">";
        if (Str_BieuThucLogic.contains("="))
            _PhepToan += "=";

        String CacDoiTuong[] = Str_BieuThucLogic.substring(1, Str_BieuThucLogic.length() - 1).split(_PhepToan);
        _DoiTuong1 = CacDoiTuong[0].trim();
        _DoiTuong2 = CacDoiTuong[1].trim();
    }

    public String jToCCode(int DoLui) {
        //String str_KetQua = jClass_BoTaoCode.jTaoCapDo(DoLui);
        String str_KetQua ="(" + _DoiTuong1 + " ";
        if (_PhepToan.equals("="))
            str_KetQua += "==";
        else if (_PhepToan.equals("<>"))
            str_KetQua += "!=";
        else
            str_KetQua += _PhepToan;
        str_KetQua += " " + _DoiTuong2 + ")";
        return str_KetQua;
    }

    public String jCSharpCode(int DoLui) {
        String str_KetQua ="(" + _DoiTuong1 + " ";
        if (_PhepToan.equals("="))
            str_KetQua += "==";
        else if (_PhepToan.equals("<>"))
            str_KetQua += "!=";
        else
            str_KetQua += _PhepToan;
        str_KetQua += " " + _DoiTuong2 + ")";
        return str_KetQua;
    }

    public String jVBNetCode(int DoLui) {

        String str_KetQua ="(" + _DoiTuong1 + " ";
        if (_PhepToan.equals("="))
            str_KetQua += "=";
        else if (_PhepToan.equals("<>"))
            str_KetQua += "<>";
        else
            str_KetQua += _PhepToan;
        str_KetQua += " " + _DoiTuong2 + ")";
        return str_KetQua;
    }

}
